//
//  MineHeaderView.swift
//  Exchange
//
//  Created by rui on 2018/5/4.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class MineHeaderView: UIView {
    
    @IBOutlet weak var trust: UILabel!
    @IBOutlet weak var good: UILabel!
    @IBOutlet weak var nickName: UILabel!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var bottomContent: UIView!
    @IBOutlet weak var arrow: UIImageView!
    
    var isLogin = false {
        didSet {
            reload()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    override func awakeFromNib() {
         super.awakeFromNib()
        self.icon.layer.cornerRadius = 27.5
        self.icon.layer.masksToBounds = true
        reload()
    }
    
    func reload() {
        self.bottomContent.isHidden = !isLogin
        self.arrow.isHidden = !isLogin
        if isLogin {
            name.text = AppInfo.default.loginName
            if let nickname = AppInfo.default.nickName {
                nickName.text = "\(kText("昵称"))：\(nickname)"
            }else {
                nickName.text = "\(kText("昵称"))：----"
            }
            if let num = AppInfo.default.trustNum {
                self.trust.text = "\(num)"
            }else {
                self.trust.text = "--"
            }
            self.good.text = AppInfo.default.reviews ?? "--"
        }else {
            name.text = kText("请登录")
            nickName.text = kText("欢迎来到Coingo.in")
            self.good.text = "--"
            self.trust.text = "--"
        }
        if let urlstr = AppInfo.default.portrait {
            icon.sd_setImage(with: URL(string: urlstr), placeholderImage: #imageLiteral(resourceName: "login_icon"))
        }else {
            icon.image = #imageLiteral(resourceName: "login_icon")
        }
    }
    
    func addTouchTarget(_ target: Any, action: Selector) {
        let tap = UITapGestureRecognizer(target: target, action: action)
        self.addGestureRecognizer(tap)
    }
    
}
